package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.UserStaffMapper;
import com.yh.homestay.pojo.UserStaff;
import com.yh.homestay.service.UserStaffService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserStaffServiceImpl implements UserStaffService {

    private Log logger = LogFactory.getLog(UserStaffServiceImpl.class);

    @Autowired
    private UserStaffMapper userStaffMapper;
    /**
     * @param page
     * @param limit
     * @param staffName
     * @Date: 2020/1/13 16:09
     * @Description: 查询出员工房间信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult showStaffs(Integer page, Integer limit, String staffName) {
        try {
            PageHelper.startPage(page, limit);
            List<UserStaff> userStaffs;
            if (StringUtils.isEmpty(staffName)) {
                userStaffs = userStaffMapper.selectAll();
            } else {
                Example example = new Example(UserStaff.class);
                example.createCriteria().andLike("staffName", "%" + staffName + "%");
                userStaffs = userStaffMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(userStaffs);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }

    }

    /**
     * @param userStaff
     * @Date: 2020/1/13 16:09
     * @Description: 插入员工信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult insertStaff(UserStaff userStaff) {
        try {
            userStaffMapper.insertSelective(userStaff);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/13 16:09
     * @Description: 删除员工信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult deleteStaff(Integer id) {
        try {
            userStaffMapper.deleteByPrimaryKey(id);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param userStaff
     * @Date: 2020/1/13 16:10
     * @Description: 更新员工信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult updateStaff(UserStaff userStaff) {
        try {
            userStaffMapper.updateByPrimaryKeySelective(userStaff);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/13 16:10
     * @Description: 查询一个员工信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult showOneStaff(Integer id) {
        try {
            UserStaff userStaff = userStaffMapper.selectByPrimaryKey(id);
            return HomeStayResult.ok(200, userStaff);
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param ids
     * @Date: 2020/1/13 16:10
     * @Description: 批量删除员工信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult delectBatchStaff(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                userStaffMapper.deleteByPrimaryKey(id);
            }
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
    * @Date: 2020/1/13 16:54
    * @Description: 返回一个员工信息，配合权限框架使用
    * @param staffid
    * @return: com.yh.homestay.pojo.UserStaff
    */
    @Override
    public UserStaff selectOneStaff(Integer staffid) {
        try{
           return userStaffMapper.selectByPrimaryKey(staffid);
        }catch (Exception e){
            logger.debug(e);
            return null;
        }
    }
}

